import {
  ease
} from '../util/ease'
import {
  PROBE_REALTIME
} from '../util/const'

export function pullDownMixin (GScroll) {
  GScroll.prototype._initPullDown = function () {
    // 定时器实时监听滚动
    this.probe = PROBE_REALTIME
  }

  GScroll.prototype._checkPullDown = function () {
    const {
      threshold = 60, stop = 40
    } = this.options.pullDownRefresh

    // 下拉方向才执行
    if (this.options.pullDownRefresh && this.y > 0 && this.y > threshold) {
      this._transitionTime(this.options.swipeBounceTime)
      this._transitionTimingFunction(ease.swipeBounce.style)
      this._translate(0, stop)

      this.pullingDown = true
      this.trigger('pullingDown')
    }

    return this.pullingDown
  }

  GScroll.prototype.finishPullDown = function () {
    this.pullingDown = false
    this.resetPosition()
  }

  GScroll.prototype.openPullDown = function (config = true) {
    this.options.pullDownRefresh = config
    this._initPullDown()
  }

  GScroll.prototype.closePullDown = function () {
    this.options.pullDownRefresh = false
  }
}
